﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;

namespace MTGenome.UI
{
    public static class ImageLoader
    {
        public static ImageSource Load(string path, int? width = null, int? height = null)
        {
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.CacheOption = BitmapCacheOption.OnLoad;
            bitmap.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);
            bitmap.EndInit();

            return Resize(bitmap, path, width, height);
        }

        private static BitmapSource Resize(BitmapImage source, string path, int? width, int? height)
        {
            BitmapSource image = null;
            const double dpi = 96;

            //Fix Dpi
            if (source.DpiX != dpi || source.DpiY != dpi)
            {
                int stride = source.PixelWidth * 4; // 4 bytes per pixel
                byte[] pixelData = new byte[stride * source.PixelHeight];
                source.CopyPixels(pixelData, stride, 0);

                image = BitmapSource.Create(source.PixelWidth, source.PixelHeight, dpi, dpi, PixelFormats.Bgra32, null, pixelData, stride);
            }

            //Fix size of image, including rotation
            if (width.HasValue && height.HasValue)
            {
                if (source.PixelWidth > source.PixelHeight)
                {
                    if (source.PixelWidth != height.Value || source.PixelHeight != width.Value)
                    {
                        TransformGroup group = new TransformGroup();
                        group.Children.Add(new RotateTransform(-90));
                        group.Children.Add(new ScaleTransform((double)width.Value / (double)source.PixelHeight, (double)height.Value / (double)source.PixelWidth));
                        image = new TransformedBitmap(image ?? source, group);
                    }
                    else
                    {
                        image = new TransformedBitmap(image ?? source, new RotateTransform(-90));
                    }
                }
                else if (source.PixelWidth != width.Value || source.PixelHeight != height.Value)
                {
                    image = new TransformedBitmap(image ?? source, new ScaleTransform((double)width.Value / (double)source.PixelWidth, (double)height.Value / (double)source.PixelHeight));
                }
            }

            if (image != null)
            {
                //Now let's save the resized image back to the cache
                SaveToCache(image, path);

                image.Freeze();
                return image;
            }
            else
            {
                source.Freeze();
                return source;
            }
        }

        private static void SaveToCache(BitmapSource image, string path)
        {
            var encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(image));
            using (FileStream stream = File.OpenWrite(path))
            {
                encoder.Save(stream);
            }
        }
    }
}
